package com.hx.springboot1.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hx.springboot1.dao.TeacherDao;
import com.hx.springboot1.dao.UserDao;
import com.hx.springboot1.pojo.dto.TeacherDTO;
import com.hx.springboot1.pojo.entity.TabTeacher;
import com.hx.springboot1.pojo.entity.TabUser;
import com.hx.springboot1.service.TeacherService;
import com.hx.springboot1.utils.PinYinUtils;
import com.hx.springboot1.utils.ResultMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Author: hx
 * Date: 2021-05-22 22/52
 * Description: Nothing else
 */
@Service
@Slf4j
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    TeacherDao teacherDao;
    @Autowired
    UserDao userDao;

    @Override
    public PageInfo selectAllTeacher(String searchName, String pageNum, String pageSize) {
        List<TeacherDTO> allTeacher;
        int pn = 1;
        int ps = 10;
        if (!StringUtils.isEmpty(pageNum)) {
            pn = Integer.parseInt(pageNum);
        }
        if (!StringUtils.isEmpty(pageSize)) {
            ps = Integer.parseInt(pageSize);
        }
        //   PageHelper  要在查询之前使用
        PageHelper.startPage(pn, ps);
        if (StringUtils.isEmpty(searchName)) {
            allTeacher = teacherDao.selectAllTeacher();
            log.info("用户{}点击teacherList页面", SecurityContextHolder.getContext().getAuthentication().getName());
        } else {
            searchName = "%" + searchName + "%";
            allTeacher = teacherDao.selectAllTeacherByName(searchName);
            log.info("{}查询了教师：{}的信息", SecurityContextHolder.getContext().getAuthentication().getName(), searchName);
        }
        PageInfo<TeacherDTO> pageInfo = new PageInfo<>(allTeacher);
        return pageInfo;
    }

    @Override
    public void insertOrUpdateTeacher(Integer teacherId, Integer userId,
                                      Integer collegeId, String teacherName, String teacherSex, String teacherJob, String teacherQua, String teacherBorn) {
        if (teacherId==null&&userId==null) {
            /* 新增教师信息 */
            // 先新增一个账号   是否首字母大写
            String name = PinYinUtils.getCamelPinYin(teacherName, false);
            userDao.insertUser(name);
            TabUser login = userDao.login(name);
            teacherDao.insertTeacher(login.getUserId(), collegeId, teacherName, teacherSex, teacherJob, teacherQua, teacherBorn);
        }else {
            teacherDao.updateTeacher(teacherId, collegeId, teacherName, teacherSex, teacherJob, teacherQua, teacherBorn);
        }
    }

    @Override
    public TabTeacher selectTeacherById(int id) {
        return teacherDao.selectByTeacherId(id);
    }

    @Override
    public ResultMsg delTeacher(String TIDAndUID) {
        int result = 0;
        if (!StringUtils.isEmpty(TIDAndUID)) {
            String[] split = TIDAndUID.split("/");
            result = teacherDao.delTeacher(Integer.parseInt(split[0]));
            userDao.updateUserState(1, Integer.parseInt(split[1]));
        }
        if (result > 0) {
            return new ResultMsg("删除成功！账户已锁定",true);
        }
        return new ResultMsg("删除失败！",false);
    }
}
